#import "SlideshowView.h"
#import "SlideshowViewController.h"
#import "LayoutManagers.h"

#define PHOTO_SPACING 40
#define SPINNER_WIDTH 40

@implementation SlideshowView

- (id)initWithFrame:(CGRect)frame controller:(SlideshowViewController *)c
{
    if (self = [super initWithFrame:frame])
    {
        controller = c;
    }
    return self;
}

- (void)dealloc
{
    [photoContainers release];
    [mainLayout release];
    [super dealloc];
}

- (void)setNumberOfPhotos:(int)n
{
    [mainLayout removeFromSuperview];
    [mainLayout release];
    mainLayout = [[HLayoutView alloc] initWithFrame:
                  CGRectMake(-PHOTO_SPACING / 2, 0, 320 + PHOTO_SPACING, 480)
                  spacing:PHOTO_SPACING
                  leftMargin:PHOTO_SPACING / 2 rightMargin:PHOTO_SPACING / 2
                  topMargin:0 bottomMargin:0
                  hAlignment:UIControlContentHorizontalAlignmentLeft
                  vAlignment:UIControlContentVerticalAlignmentTop];
    mainLayout.scrollEnabled = YES;
    mainLayout.pagingEnabled = YES;
    mainLayout.showsHorizontalScrollIndicator = NO;
    mainLayout.showsVerticalScrollIndicator = NO;
    mainLayout.delegate = self;
    [self addSubview:mainLayout];
    [photoContainers release];
    photoContainers = [[NSMutableArray alloc] initWithCapacity:n];
    for (int i = 0; i < n; ++i)
    {
        UIView *container = [[UIView alloc] initWithFrame:
                             CGRectMake(0, 0, 320, 480)];
        // scrolling doesn't work without the next line - why??
        container.backgroundColor = [UIColor blackColor];
        [mainLayout addSubview:container];
        [container release];
        [container addSubview:[self loadingIndicatorView]];
        [photoContainers addObject:container];
    }
    // force layout, because -showPhotoAtIndex: can be called before displaying
    [mainLayout layoutSubviews];
}

- (void)setPhoto:(UIImage *)photo atIndex:(int)index
{
    UIView *photoContainer = [photoContainers objectAtIndex:index];
    UIView *loadingIndicator = [photoContainer.subviews lastObject];
    [loadingIndicator removeFromSuperview];
    float width = photo.size.width;
    float height = photo.size.height;
    float widthRatio = 320.0 / width;
    float heightRatio = 480.0 / height;
    if (widthRatio < heightRatio)
    {
        width *= widthRatio;
        height *= widthRatio;
    }
    else
    {
        width *= heightRatio;
        height *= heightRatio;
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                              CGRectMake((320 - width) / 2, (480 - height) / 2,
                                         width, height)];
    imageView.image = photo;
    [photoContainer addSubview:imageView];
    [imageView release];
}

- (void)showPhotoAtIndex:(int)index
{
    [mainLayout scrollRectToVisible:
     CGRectMake((320 + PHOTO_SPACING) * index, 0, 320 + PHOTO_SPACING, 480)
     animated:NO];
}

- (UIView *)loadingIndicatorView
{
    UIActivityIndicatorView *spinner
        = [[UIActivityIndicatorView alloc] initWithFrame:
           CGRectMake((320 - SPINNER_WIDTH) / 2,
                      (480 - SPINNER_WIDTH) / 2,
                      SPINNER_WIDTH, SPINNER_WIDTH)];
    spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [spinner startAnimating];
    return [spinner autorelease];
}

#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int x = mainLayout.bounds.origin.x;
    if ((x % (320 + PHOTO_SPACING)) == 0)
        [controller scrolledToIndex:x / (320 + PHOTO_SPACING)];
}

@end
